package de.herzog.bbp.view.javafx;

import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import javafx.scene.control.TableCell;

public abstract class AutoCompleteListFactory<S, T> {

	private TableCell<S, T> tableCell;
	protected Function<T, String> mapper;

	public AutoCompleteListFactory(Function<T, String> mapper) {
		super();
		this.mapper = mapper;
	}

	@SuppressWarnings("unchecked")
	protected S getRowBean() {
		return (S) tableCell.getTableRow().getItem();
	}

	public void setTableCell(TableCell<S, T> tableCell) {
		this.tableCell = tableCell;
	}

	public List<String> getList() {
		return getObjectList().stream().map(mapper).collect(Collectors.toList());
	}

	protected abstract List<T> getObjectList();

	protected List<T> sort(List<T> list) {
		list.sort(new Comparator<T>() {
			@Override
			public int compare(T o1, T o2) {
				String s1 = mapper.apply(o1);
				String s2 = mapper.apply(o2);
				return s1.compareTo(s2);
			}
		});
		return list;
	}
}
